package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RequestMapping("/emps")
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;


    /***
     * 查询所有员工
     */
    @GetMapping("/list")
    public Result list(){
        List<Emp> list = empService.list();
        return Result.success(list);
    }

    /***
     * 修改操作
     */
    @PutMapping
    public Result update(@RequestBody Emp emp){
        String num = "abc";
        Integer.parseInt(num);
        // 调用Service完成修改
        empService.update(emp);
        return Result.success();
    }


    /***
     * 修改员工信息-员工信息回显
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        //调用Service执行查询
        Emp emp = empService.getInfo(id);
        return Result.success(emp);
    }


    /****
     * 批量删除员工
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        //调用业务层实现批量删除
        empService.delete(ids);
        return Result.success();
    }


    /***
     * 新增员工信息
     */
    @PostMapping
    public Result save(@RequestBody Emp emp) throws Exception {
        empService.save(emp);
        return Result.success();
    }



    /***
     * 分页查询
     */
    @GetMapping
    //public Result page(@RequestParam(defaultValue = "1")Integer page,
    //                   @RequestParam(defaultValue = "10")Integer pageSize,
    //                   String name,Integer gender,
    //                   // 接收前端指定格式的日期参数，并转成Date类型
    //                   @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate begin,
    //                   @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end){
    public Result page(EmpQueryParam empQueryParam){
        // 调用Service查询->PageResult
        //PageResult pageResult = empService.page(page,pageSize,name,gender,begin,end);
        PageResult pageResult = empService.page(empQueryParam);
        //响应数据
        return Result.success(pageResult);
    }


}
